package com.resource.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.resource.constant.OfficeApiConstants;
import com.resource.enums.ProcessFormEnum;
import com.resource.response.OfficeProjectInfoResponse;
import com.resource.response.OfficeResponse;
import com.resource.utils.HttpUtils;
import com.resource.utils.ValidateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.resource.service.OfficeProcessFormService;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 流程表单业务处理
 * 
 * @Author: xianggaoxiong
 * @Date: 2020-11-12 14:14
 **/
@Slf4j
@Service
public class OfficeProcessFormServiceImpl implements OfficeProcessFormService {
	@Resource
	private RestTemplate restTemplate;
	@Value("${office.app.user}")
	private String user;
	@Value("${office.api.url}")
	private String apiUrl;

	@Override
	public List<JSONObject> getTravelInfos(Integer formId, String runIds) {
		List<JSONObject> response = new ArrayList<>();
		String[] split = StrUtil.split(runIds, ",");
		List<String> strings = Arrays.asList(split);
		strings.forEach(e -> {
			StringBuilder url = new StringBuilder(String.format(OfficeApiConstants.PROCESS_FORM_DATA_URL, apiUrl));
			url.append("?status=view").append("&formId=").append(formId).append("&runId=").append(e);
			HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.GET, "", user);
			ResponseEntity<OfficeResponse> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.GET,
					entity, OfficeResponse.class);
			log.info("【office】getTravelInfos process formData return：{}", JSONUtil.toJsonStr(responseEntity));
			// 校验返回http请求返回信息
			OfficeResponse officeResponse = ValidateUtils.checkResponse(responseEntity);
			JSONObject jsonObject = new JSONObject();
			jsonObject.putOpt("DATA_1112_0",
					JSONUtil.parseObj(officeResponse.getData()).getByPath("$.parseData.DATA_10"));
			jsonObject.putOpt("DATA_1112_7",
					JSONUtil.parseObj(officeResponse.getData()).getByPath("$.parseData.DATA_11"));
			jsonObject.putOpt("DATA_1112_8",
					JSONUtil.parseObj(officeResponse.getData()).getByPath("$.parseData.DATA_73"));
			jsonObject.putOpt("DATA_1112_9",
					JSONUtil.parseObj(officeResponse.getData()).getByPath("$.parseData.DATA_74"));
			response.add(jsonObject);
		});
		return response;
	}

	@Override
	public JSONObject getReceiver(ProcessFormEnum processFormEnum, String runId) {
		StringBuilder url = new StringBuilder(String.format(OfficeApiConstants.PROCESS_FORM_DATA_URL, apiUrl));
		url.append("?status=view&formId=").append(processFormEnum.getType()).append("&runId=").append(runId);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.GET, "", user);
		ResponseEntity<OfficeResponse> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.GET, entity,
				OfficeResponse.class);
		log.info("【office】getReceiver process formData return：{}", JSONUtil.toJsonStr(responseEntity));
		OfficeResponse officeResponse = ValidateUtils.checkResponse(responseEntity);
		JSONObject formData = JSONUtil.parseObj(officeResponse.getData());
		JSONObject result = new JSONObject();
        //判断接待单据类型：市场接待或公司接待返回的单据申请人属性是不同的
		if (processFormEnum.equals(ProcessFormEnum.MARKET_RECEPTION)) {
            result.putOpt("DATA_1147", Convert.toStrArray(formData.getByPath("$.parseData.DATA_17"))[0]);
		} else {
            result.putOpt("DATA_1147", formData.getByPath("$.parseData.DATA_1"));
		}
		return result;
	}
}
